package com.desaysv.svview.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Vector;

/**
 * 通用适配器
 *
 * @author uidq5208
 * @version 1.0
 * @date 2022-12-13 13:36
 * @since 2022-12-13 13:36
 */
public abstract class BaseRecycleAdapter extends RecyclerView.Adapter<BaseRecycleAdapter.HolderProxy> {
    /**
     * 日志tag
     */
    private static final String TAG = "[BaseRecycleAdapter]";

    /**
     * 设置上下文对象
     *
     * @return Context
     */
    public abstract Context setContext();

    /**
     * 布局文件
     *
     * @return LayoutRes
     */
    public abstract @LayoutRes
    int setView();

    public abstract @LayoutRes
    int setView(int position);

    /**
     * 数据长度
     *
     * @return 长度
     */
    public abstract int setSize();

    /**
     * 绑定数据
     *
     * @param item     每个item
     * @param position 下标
     */
    public abstract void bind(HolderProxy item, int position);

    /**
     * 响应点击事件
     */
    private final Vector<Integer> mClickRes = new Vector<>();
    /**
     * 点击事件
     */
    private OnItemClickListener onItemClickListener;
    /**
     * 是否整个item响应点击
     */
    private boolean rootClick = false;

    /**
     * 构造函数
     */
    public BaseRecycleAdapter() {
    }

    @SuppressLint("ResourceType")
    @SuppressWarnings("checkstyle:WhitespaceAround")
    @NonNull
    @Override
    public BaseRecycleAdapter.HolderProxy onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        int res = setView(i);
        if (res <= 0) {
            res = setView();
        }
        HolderProxy hp = new HolderProxy(viewGroup, res);
        Log.i(TAG, "onCreateViewHolder: register click");
        for (Integer mClickRe : mClickRes) {
            hp.getView(mClickRe).setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.itemClick(v, i);
                }
            });
        }
        Log.i(TAG, "onCreateViewHolder: register rootItem");
        if (rootClick) {
            hp.itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.itemClick(v, hp.getLayoutPosition());
                }
            });
        }
        return hp;
    }

    /**
     * 设置点击事件回调
     *
     * @param onItemClickListener 回调接口
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 设置是否根层级点击
     *
     * @param rootClick true/false
     */
    public void setRootClick(boolean rootClick) {
        this.rootClick = rootClick;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseRecycleAdapter.HolderProxy viewHolder, int i) {
        bind(viewHolder, i);
    }

    /**
     * 通过控件id绑定点击事件
     *
     * @param viewId 控件id
     */
    public void registerClick(@IdRes int viewId) {
        mClickRes.addElement(viewId);
    }

    @Override
    public int getItemCount() {
        return setSize();
    }

    /**
     * Holder代理类
     *
     * @author uidq5208
     * @date 2022-12-13 13:40
     */
    public class HolderProxy extends RecyclerView.ViewHolder {

        /**
         * 构造函数
         *
         * @param itemView item视图
         */
        public HolderProxy(@NonNull View itemView) {
            super(itemView);
        }

        /**
         * 构造函数
         *
         * @param layoutId 资源布局id
         */
        public HolderProxy(@LayoutRes int layoutId) {
            this(LayoutInflater.from(setContext()).inflate(layoutId, null, false));
        }

        /**
         * 构造函数
         *
         * @param group    根布局
         * @param layoutId 资源id
         */
        public HolderProxy(ViewGroup group, @LayoutRes int layoutId) {
            this(LayoutInflater.from(setContext()).inflate(layoutId, group, false));
        }

        /**
         * 根据资源id设置显示与隐藏
         *
         * @param resId   资源id
         * @param visible 是否显示隐藏
         */
        public void setVisible(@IdRes int resId, int visible) {
            View view = itemView.findViewById(resId);
            view.setVisibility(visible);
        }

        /**
         * 设置text显示字符串
         *
         * @param resId 控件id
         * @param cs    字符信息
         */
        public void setText(@IdRes int resId, CharSequence cs) {
            View view = itemView.findViewById(resId);
            if (view instanceof TextView) {
                ((TextView) view).setText(cs);
            } else if (view instanceof Button) {
                ((Button) view).setText(cs);
            } else if (view instanceof RadioButton) {
                ((RadioButton) view).setText(cs);
            }
        }

        /**
         * 设置text显示字符串
         *
         * @param resId 控件id
         * @param cs    字符resId信息
         */
        public void setText(@IdRes int resId, @StringRes int cs) {
            View view = itemView.findViewById(resId);
            if (view instanceof TextView) {
                ((TextView) view).setText(cs);
            } else if (view instanceof Button) {
                ((Button) view).setText(cs);
            } else if (view instanceof RadioButton) {
                ((RadioButton) view).setText(cs);
            }
        }

        /**
         * 设置背景显示资源
         *
         * @param resId 控件id
         * @param bg    背景信息resId
         */
        public void setBackground(@IdRes int resId, @DrawableRes int bg) {
            View view = itemView.findViewById(resId);
            view.setBackground(setContext().getDrawable(bg));
        }

        /**
         * 获取控件
         *
         * @param resId 控件id
         * @return View
         */
        public <T extends View> T findView(@IdRes int resId) {
            return (T) itemView.findViewById(resId);
        }

        public View getView(@IdRes int resId) {
            return itemView.findViewById(resId);
        }
    }

    /**
     * 点击事件回调接口
     *
     * @author uidq5208
     * @date 2022-12-13 13:49
     */
    public interface OnItemClickListener {
        /**
         * 回调函数
         *
         * @param v        视图
         * @param position 下标
         */
        void itemClick(View v, int position);
    }
}
